﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class NhomQuyenProvider
    {
        public static List<NhomQuyen> LayDanhSachNhomQuyen()
        {
            var lst = new List<NhomQuyen>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.NhomQuyens select u);

                foreach (var row in query)
                {
                    var nhomQuyen = new NhomQuyen();
                    nhomQuyen.MaNhomQuyen = row.MaNhomQuyen;
                    nhomQuyen.TenNhom = row.TenNhom;
                    nhomQuyen.MoTa = row.MoTa;

                    lst.Add(nhomQuyen);
                }
            }

            return lst;
        }

        public static NhomQuyen LayNhomQuyen(int maNhom)
        {
            var nhom = new NhomQuyen();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.NhomQuyens select  u)
                    .Where(t => t.MaNhomQuyen.Equals(maNhom))
                    .First() ;

                if (query != null)
                {
                    nhom = query;
                }
                else
                {
                    nhom = null;
                }
            }

            return nhom;
        }

        public static bool XoaNhomQuyen(int maNhomQ)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {

                    var query = (from u in context.NhomQuyens
                                 where u.MaNhomQuyen.Equals(maNhomQ)
                                 select u).First();

                    context.NhomQuyens.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatNhomQuyen(NhomQuyen nhomQuyen)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.NhomQuyens
                                 where u.MaNhomQuyen.Equals(nhomQuyen.MaNhomQuyen)
                                 select u).First();
                    query.TenNhom = nhomQuyen.TenNhom;
                    query.MoTa = nhomQuyen.MoTa;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static int ThemNhomQuyen(NhomQuyen nhomQuyen)
        {
            int maNhomQ = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.NhomQuyens.Add(nhomQuyen);
                    context.SaveChanges();
                    maNhomQ = nhomQuyen.MaNhomQuyen;
                }
            }
            catch
            {

            }
            return maNhomQ;
        }
    }
}
